package model

import (
	"gorm.io/gorm"
	__ "video-srv/basic/proto/rpc"
)

type Video struct {
	gorm.Model
	UserId     int    `gorm:"type:int" json:"userId"`
	Title      string `gorm:"type:varchar(30)" json:"title"`
	Image      string `gorm:"type:varchar(30)" json:"image"`
	Refer      string `gorm:"type:varchar(30)" json:"refer"`
	Status     int    `gorm:"type:int;default:0" json:"status"`
	AreaId     int    `gorm:"type:int;default:0" json:"areaId"`
	CategoryId int    `gorm:"type:int;default:0" json:"categoryId"`
	ChannelId  int    `gorm:"type:int;default:0" json:"channelId"`
}

type CheckVideo struct {
	gorm.Model
	UserId  int    `gorm:"type:int" json:"userId"`
	VideoId int    `gorm:"type:int" json:"videoId"`
	Status  int    `gorm:"type:int" json:"status"`
	Reason  string `gorm:"type:varchar(30)" json:"reason"`
}

type Vip struct {
	gorm.Model
	UserId  int    `gorm:"type:int" json:"userId"`
	Status  int    `gorm:"type:int" json:"status"`          //会员状态
	TimeOut string `gorm:"type:varchar(30)" json:"timeOut"` //过期时间
}

type Area struct {
	gorm.Model
	AreaName string `gorm:"type:varchar(30)" json:"areaName"`
}

type Category struct {
	gorm.Model
	CategoryName string `gorm:"type:varchar(30)" json:"categoryName"`
}

type Channel struct {
	gorm.Model
	ChannelName string `gorm:"type:varchar(30)" json:"channelName"`
}

func (v *Video) PublishVideo(db *gorm.DB) error {
	return db.Create(&v).Error
}

func (c *CheckVideo) CheckVideo(db *gorm.DB) error {
	return db.Create(&c).Error
}

func (v *Video) UpdateVideoStatus(db *gorm.DB, videoId, status int) error {
	return db.Model(&v).Where("id = ?", videoId).Update("status", status).Error
}

func (v *Video) GetVideoListById(db *gorm.DB, videoId int) (list *__.VideoItem, err error) {
	err = db.Model(&v).
		Select("videos.id,title,image,videos.refer,videos.`status`,areas.area_name,categories.category_name,channels.channel_name").
		Joins("LEFT JOIN areas ON areas.id = videos.area_id").
		Joins("LEFT JOIN categories ON categories.id = videos.category_id").
		Joins("LEFT JOIN channels ON channels.id = videos.channel_id").
		Where("id = ?", videoId).First(&list).Error
	return
}

func (v *Video) GetVideoList(db *gorm.DB) (list []*__.VideoItem, err error) {
	err = db.Model(&v).
		Select("videos.id,title,image,videos.refer,videos.`status`,areas.area_name,categories.category_name,channels.channel_name").
		Joins("LEFT JOIN areas ON areas.id = videos.area_id").
		Joins("LEFT JOIN categories ON categories.id = videos.category_id").
		Joins("LEFT JOIN channels ON channels.id = videos.channel_id").
		Find(&list).Error
	return
}
